package com.tf.integrated.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.config.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 以图搜图-行人接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "以图搜图-行人接口",description="SimilarPedestrainController")
@RestController
@RequestMapping("/similarPedestrain")
public class SimilarPedestrainController extends BaseController
{
    @ApiOperation("上传远程目标图片-行人")
    @PostMapping("/remote")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSON remote(@RequestParam Map<String, String> data) throws Exception {
        String disUrl="/similarPedestrain/remote";
        if(data!=null&&data.size()>0&&isExist(data.get("url"))&&isExist(data.get("fileName"))) {
            FileSystemResource resource = new FileSystemResource((Config.createFile(data.get("url"),data.get("fileName"))));
            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
            param.add("file", resource);
            HttpMethod method=HttpMethod.POST;
            if(isExist(data.get("host"))){
                String host = data.get("host");
                HttpHeaders headers = new HttpHeaders();
                headers.set("X-Token", Config.getmCurtoken(host));
                String response = Config.clientWithFormData(Config.getStarturlpre(host)+disUrl, method, param, headers);
                LogController.add(disUrl,LogController.METHOD_POST,1);
                return JSONObject.parseObject(response);
            }else{
                JSONArray array=new JSONArray();
                for(Map.Entry<String, String> entry : Config.getmDevToken().entrySet()){
                    String deviceId=entry.getKey();
                    HttpHeaders headers=new HttpHeaders();
                    headers.set("X-Token", Config.getmCurtoken(deviceId));
                    String response = Config.clientWithFormData(Config.getStarturlpre(deviceId)+disUrl, method, param, headers);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("deviceId",deviceId);
                    jsonObject.put("info",JSONObject.parseObject(response));
                    array.add(jsonObject);
                }
                LogController.add(disUrl,LogController.METHOD_POST,1);
                return array;
            }
        }
        LogController.add(disUrl,LogController.METHOD_POST,0);
        return new JSONObject();
    }

    @ApiOperation("以图搜图进度")
    @GetMapping("/progress")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSON select(@RequestParam Map<String, String> data) {
        String disUrl="/similarPedestrain/progress";
        HttpMethod method=HttpMethod.GET;
        if(data!=null&&data.size()>0&&isExist(data.get("host"))) {
            String host = data.get("host");
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host)+disUrl,method,null,headers);
            LogController.add(disUrl,LogController.METHOD_GET,1);
            return JSONObject.parseObject(analyseResults);
        }else{
            JSONArray array=new JSONArray();
            for(Map.Entry<String, String> entry : Config.getmDevToken().entrySet()){
                String deviceId=entry.getKey();
                HttpHeaders headers=new HttpHeaders();
                headers.set("X-Token", Config.getmCurtoken(deviceId));
                String analyseResults = Config.client(Config.getStarturlpre(deviceId)+disUrl,method,null,headers);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("deviceId",deviceId);
                jsonObject.put("info",JSONObject.parseObject(analyseResults));
                array.add(jsonObject);
            }
            LogController.add(disUrl,LogController.METHOD_GET,1);
            return array;
        }
    }

    @ApiOperation("以图搜图分析结果")
    @GetMapping("/result")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSON result(@RequestParam Map<String, String> data) {
        String disUrl="/similarPedestrain/result?";
        if(data!=null&&data.size()>0){
            HttpMethod method = HttpMethod.GET;
            disUrl=disUrl+Config.buildMap(data);
            if(isExist(data.get("host"))){
                String host = data.get("host");
                HttpHeaders headers = new HttpHeaders();
                headers.set("X-Token", Config.getmCurtoken(host));
                String analyseResults = Config.client(Config.getStarturlpre(host)+disUrl,method,null,headers);
                LogController.add(disUrl,LogController.METHOD_GET,1);
                return JSONObject.parseObject(analyseResults);
            }else {
                JSONArray array = new JSONArray();
                for (Map.Entry<String, String> entry : Config.getmDevToken().entrySet()) {
                    String deviceId = entry.getKey();
                    HttpHeaders headers = new HttpHeaders();
                    headers.set("X-Token", Config.getmCurtoken(deviceId));
                    String analyseResults = Config.client(Config.getStarturlpre(deviceId) + disUrl, method, null, headers);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("deviceId", deviceId);
                    jsonObject.put("info", JSONObject.parseObject(analyseResults));
                    array.add(jsonObject);
                }
                LogController.add(disUrl, LogController.METHOD_GET, 1);
                return array;
            }
        }
        LogController.add(disUrl,LogController.METHOD_GET,0);
        return new JSONObject();
    }

    @ApiOperation("开始以图搜图")
    @PostMapping("/start")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSON start(@RequestParam Map<String, String> data) {
        String disUrl="/similarPedestrain/start?";
        if(data!=null&&data.size()>0){
            HttpMethod method = HttpMethod.POST;
            disUrl=disUrl+Config.buildMap(data);
            if(isExist(data.get("host"))){
                String host = data.get("host");
                HttpHeaders headers = new HttpHeaders();
                headers.set("X-Token", Config.getmCurtoken(host));
                String analyseResults = Config.client(Config.getStarturlpre(host)+disUrl,method,null,headers);
                LogController.add(disUrl,LogController.METHOD_POST,1);
                return JSONObject.parseObject(analyseResults);
            }else {
                JSONArray array = new JSONArray();
                for (Map.Entry<String, String> entry : Config.getmDevToken().entrySet()) {
                    String deviceId = entry.getKey();
                    HttpHeaders headers = new HttpHeaders();
                    headers.set("X-Token", Config.getmCurtoken(deviceId));
                    String analyseResults = Config.client(Config.getStarturlpre(deviceId) + disUrl, method, null, headers);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("deviceId", deviceId);
                    jsonObject.put("info", JSONObject.parseObject(analyseResults));
                    array.add(jsonObject);
                }
                LogController.add(disUrl, LogController.METHOD_POST, 1);
                return array;
            }
        }
        LogController.add(disUrl,LogController.METHOD_POST,0);
        return new JSONObject();
    }

    @ApiOperation("停止以图搜图")
    @PostMapping("/stop")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSON stop(@RequestParam Map<String, String> data) {
        String disUrl="/similarPedestrain/stop";
        HttpMethod method=HttpMethod.POST;
        if(data!=null&&data.size()>0&&isExist(data.get("host"))) {
            String host = data.get("host");
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host)+disUrl,method,null,headers);
            LogController.add(disUrl,LogController.METHOD_POST,1);
            return JSONObject.parseObject(analyseResults);
        }else{
            JSONArray array=new JSONArray();
            for(Map.Entry<String, String> entry : Config.getmDevToken().entrySet()){
                String deviceId=entry.getKey();
                HttpHeaders headers=new HttpHeaders();
                headers.set("X-Token", Config.getmCurtoken(deviceId));
                String analyseResults = Config.client(Config.getStarturlpre(deviceId)+disUrl,method,null,headers);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("deviceId",deviceId);
                jsonObject.put("info",JSONObject.parseObject(analyseResults));
                array.add(jsonObject);
            }
            LogController.add(disUrl,LogController.METHOD_POST,1);
            return array;
        }
    }

    public boolean isExist(String value){
        return value != null && !value.equals("");
    }
}